package cn.wolfcode.luowowo.member.service.impl;

import cn.wolfcode.luowowo.cache.service.IVerifyCodeService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.common.util.Assert;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Reference
    private IVerifyCodeService verifyCodeService;

    public Boolean checkPhone(String phone) {

        return userInfoMapper.selectByPhone(phone)==null;
    }

    //要什么返回什么?
    public AjaxResult userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) {

        AjaxResult result = new AjaxResult();
        try {

            //1.判断是否为空
            Assert.hasLength(phone,"手机号不能为空");
            Assert.hasLength(nickname,"昵称不能为空");
            Assert.hasLength(password,"密码不能为空");
            Assert.hasLength(rpassword,"确认密码不能为空");
            Assert.hasLength(verifyCode,"验证码不能为空");
            //2.验证码校验
            String cacheverifyCode = verifyCodeService.getVerifyCode(phone);
            //忽略大小写.
            Assert.equalsIgnoreCase(verifyCode,cacheverifyCode,"验证码不匹配,请重新发送");


            //3.,通过保存后,保存默认数据
            UserInfo userInfo = new UserInfo();
            userInfo.setPhone(phone);
            userInfo.setNickname(nickname);
            userInfo.setPassword(password);
            //默认数据
            userInfo.setEmail("123@123.com");
            userInfo.setLevel(1);
            userInfo.setBirthday(new Date());
            userInfo.setGender(UserInfo.GENDER_FEMALE);
            userInfo.setCity("东京");
            userInfo.setInfo("大佬的牛逼不是普通人理解的");
            userInfo.setHeadImgUrl("/images/xiaoyao.jpg");


            //4,保存注册信息
            userInfoMapper.insert(userInfo);

        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }

        return result;
    }

    public AjaxResult userLogin(String phone, String password) {
        AjaxResult result = new AjaxResult();
        try {  //使用try.catch,防止程序出现错误,找不到.
            //1.检查数据合法性,
            Assert.hasLength(phone,"手机号不能为空");
            Assert.hasLength(password,"密码不能为空");


        UserInfo userInfo=userInfoMapper.selectByInfo(phone,password);
        //登录失败
        if(userInfo==null){
            throw  new RuntimeException("手机号或密码错误");
        }
        //登录成功,把信息返回给website.
            result.addData(userInfo);

        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    public List<UserInfo> list() {
        return userInfoMapper.list();
    }

}
